package com.example.controller;

import com.example.common.api.ApiResult;
import com.example.entity.Employee;
import com.example.entity.EmpEducation;
import com.example.entity.EmpWorkHistory;
import com.example.entity.EmpCertificate;
import com.example.service.EmployeeService;
import com.example.annotation.Log;
import com.example.dto.EmployeeDTO;
import com.example.enums.BusinessType;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;

/**
 * 员工信息管理
 */
@Api(tags = "员工管理接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 获取员工列表
     */
    @ApiOperation("获取员工列表")
    @SaCheckPermission("employee:list")
    @GetMapping("/list")
    public ApiResult<List<Employee>> list(@ApiParam("员工信息") Employee employee) {
        List<Employee> list = employeeService.list();
        return ApiResult.success(list);
    }

    /**
     * 获取员工详细信息
     */
    @ApiOperation("获取员工详细信息")
    @SaCheckPermission("employee:query")
    @GetMapping("/{employeeId}")
    public ApiResult<Employee> getInfo(@ApiParam("员工ID") @PathVariable Long employeeId) {
        Employee employee = employeeService.getEmployeeDetail(employeeId);
        return ApiResult.success(employee);
    }

    /**
     * 获取员工教育经历
     */
    @ApiOperation("获取员工教育经历")
    @SaCheckPermission("employee:query")
    @GetMapping("/{employeeId}/education")
    public ApiResult<List<EmpEducation>> getEducation(@ApiParam("员工ID") @PathVariable Long employeeId) {
        List<EmpEducation> list = employeeService.getEducationList(employeeId);
        return ApiResult.success(list);
    }

    /**
     * 获取员工工作经历
     */
    @ApiOperation("获取员工工作经历")
    @SaCheckPermission("employee:query")
    @GetMapping("/{employeeId}/work-history")
    public ApiResult<List<EmpWorkHistory>> getWorkHistory(@ApiParam("员工ID") @PathVariable Long employeeId) {
        List<EmpWorkHistory> list = employeeService.getWorkHistoryList(employeeId);
        return ApiResult.success(list);
    }

    /**
     * 获取员工证书信息
     */
    @ApiOperation("获取员工证书信息")
    @SaCheckPermission("employee:query")
    @GetMapping("/{employeeId}/certificate")
    public ApiResult<List<EmpCertificate>> getCertificate(@ApiParam("员工ID") @PathVariable Long employeeId) {
        List<EmpCertificate> list = employeeService.getCertificateList(employeeId);
        return ApiResult.success(list);
    }

    /**
     * 新增员工信息
     */
    @ApiOperation("新增员工信息")
    @SaCheckPermission("employee:add")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult<Void> add(@ApiParam("员工信息") @Validated @RequestBody EmployeeDTO employeeDTO) {
        boolean success = employeeService.insertEmployee(
            employeeDTO.getEmployee(),
            employeeDTO.getEducationList(),
            employeeDTO.getWorkHistoryList(),
            employeeDTO.getCertificateList()
        );
        return success ? ApiResult.success() : ApiResult.failed("新增员工失败");
    }

    /**
     * 修改员工信息
     */
    @ApiOperation("修改员工信息")
    @SaCheckPermission("employee:edit")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult<Void> edit(@ApiParam("员工信息") @Validated @RequestBody EmployeeDTO employeeDTO) {
        employeeService.updateEmployee(employeeDTO);
        return ApiResult.success();
    }

    /**
     * 删除员工信息
     */
    @ApiOperation("删除员工信息")
    @SaCheckPermission("employee:remove")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeId}")
    public ApiResult<Void> remove(@ApiParam("员工ID") @PathVariable Long employeeId) {
        employeeService.deleteEmployee(employeeId);
        return ApiResult.success();
    }

    /**
     * 员工转正
     */
    @ApiOperation("员工转正")
    @SaCheckPermission("employee:regular")
    @Log(title = "员工转正", businessType = BusinessType.UPDATE)
    @PutMapping("/{employeeId}/regular")
    public ApiResult<Void> confirmRegular(
            @ApiParam("员工ID") @PathVariable Long employeeId,
            @ApiParam("转正日期") @RequestParam Date regularDate) {
        boolean success = employeeService.confirmRegular(employeeId, regularDate);
        return success ? ApiResult.success() : ApiResult.failed("员工转正操作失败");
    }

    /**
     * 员工离职
     */
    @ApiOperation("员工离职")
    @SaCheckPermission("employee:leave")
    @Log(title = "员工离职", businessType = BusinessType.UPDATE)
    @PutMapping("/{employeeId}/leave")
    public ApiResult<Void> employeeLeave(
            @ApiParam("员工ID") @PathVariable Long employeeId,
            @ApiParam("离职日期") @RequestParam Date leaveDate,
            @ApiParam("离职原因") @RequestParam String leaveReason) {
        boolean success = employeeService.employeeLeave(employeeId, leaveDate, leaveReason);
        return success ? ApiResult.success() : ApiResult.failed("员工离职操作失败");
    }
} 